package copy;

import java.io.*;

public class FileCopyTest {
    public static void main(String[] args) {
        // 设置源文件路径和目标文件路径
        String sourceFilePath = "src/main/resources/example.mp3";
        String destinationFilePathForByteStream = "src/main/resources/example_byte_stream.mp3";
        String destinationFilePathForBufferedStream = "src/main/resources/example_buffered_stream.mp3";

        // 检查源文件是否存在
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            System.err.println("Source file does not exist: " + sourceFilePath);
            return;
        }

        long startTime, endTime;

        // 测试字节流复制
        startTime = System.currentTimeMillis();
        try {
            System.out.println("Starting byte stream copy from " + sourceFilePath + " to " + destinationFilePathForByteStream);
            FileCopyWithByteStream.copyFileUsingByteStream(sourceFilePath, destinationFilePathForByteStream);
            System.out.println("Byte stream copy completed successfully.");
        } catch (IOException e) {
            System.err.println("Error during byte stream copy: " + e.getMessage());
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();
        System.out.println("Time taken by Byte Stream: " + (endTime - startTime) + " ms");

        // 测试字节缓冲流复制
        startTime = System.currentTimeMillis();
        try {
            System.out.println("Starting buffered stream copy from " + sourceFilePath + " to " + destinationFilePathForBufferedStream);
            FileCopyWithBufferedStream.copyFileUsingBufferedStream(sourceFilePath, destinationFilePathForBufferedStream);
            System.out.println("Buffered stream copy completed successfully.");
        } catch (IOException e) {
            System.err.println("Error during buffered stream copy: " + e.getMessage());
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();
        System.out.println("Time taken by Buffered Stream: " + (endTime - startTime) + " ms");
    }
}

class FileCopyWithByteStream {
    public static void copyFileUsingByteStream(String sourceFilePath, String destinationFilePath) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(sourceFilePath);
            fos = new FileOutputStream(destinationFilePath);
            int byteRead;
            while ((byteRead = fis.read()) != -1) {
                fos.write(byteRead);
            }
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

class FileCopyWithBufferedStream {
    public static void copyFileUsingBufferedStream(String sourceFilePath, String destinationFilePath) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(sourceFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destinationFilePath));
            byte[] buffer = new byte[8192]; // 增大缓冲区大小
            int bytesRead;
            while ((bytesRead = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            System.err.println("Error during buffered stream copy: " + e.getMessage());
            throw e; // 重新抛出异常以便主方法捕获
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }
}
